const app = getApp()

Page({
    data:{
        array:[
            {
                id:0,
                name:'请选择'
            },
            {
                id: 1,
                name: '铝模优秀班组'
            },
            {
                id: 2,
                name: '市政优秀班组'
            },
            {
                id: 3,
                name: '土建优秀班组'
            },
            {
                id: 4,
                name: '其他'
            }
        ],
        typesArray:[
            {
                id: 2,
                name: '铝模工'
            },
            {
                id: 4,
                name: '木工'
            },
            {
                id: 5,
                name: '钢筋工'
            },
            {
                id: 6,
                name: '混凝土工'
            },
            {
                id: 7,
                name: '架子工'
            },
            {
                id: 8,
                name: '水电工'
            },{
                id: 9,
                name: '泥瓦工'
            },
            {
                id: 10,
                name: '铝合金不锈钢工'
            },
            {
                id: 11,
                name: '幕墙工'
            },
            {
                id: 12,
                name: '杂工'
            },
            {
                id: 13,
                name: '杂工'
            },{
                id: 14,
                name: '园林绿化'
            },{
                id: 15,
                name: '道路桥梁'
            },{
                id: 16,
                name: '城市照明'
            },{
                id: 17,
                name: '通讯工程'
            },{
                id: 18,
                name: '给排水'
            },{
                id: 19,
                name: '水电'
            },{
                id: 20,
                name: '油漆'
            },{
                id: 21,
                name: '泥水'
            },{
                id: 22,
                name: '其他'
            },
        ],
        region:[{code:0,name:'省'},{code:0,name:'市'},{code:0,name:'区'}],
        holp:[{code:0,name:'省'},{code:0,name:'市'},{code:0,name:'区'}],
        count:1,
        name:'',
        type:0,
        men:0,
        project:[],
        avatar:'',
        phone:'',
        types:''
    },
    bindPickerChange:function(e) {
        console.log(e.detail.value)
        this.setData({
            type: e.detail.value
        })
    },
    bindPickerTypeChange:function(e){
        this.setData({
            types: e.detail.value
        })
    },
    bindCityChange:function(e){
        //
        // let data = this.cityCode(e.detail);
        // console.log(data);
        this.setData({
            region:this.cityCode(e.detail)
        })
    },
    bindHolpCityChange:function(e){
        this.setData({
            holp:this.cityCode(e.detail)
        })
    },
    addItem(){
        this.setData({
            count:++this.data.count
        })
    },
    inputName(e){
        this.setData({
            name:e.detail.value
        })
    },
    inputPhone(e){
        this.setData({
            phone:e.detail.value
        })
    },
    cityCode(obj){
        let codeArray = obj.code
        let valueArray = obj.value
        let data = [];
        for (let i=0;i<3;i++){
            let obj = {}
            obj.code = codeArray[i];
            obj.name = valueArray[i];
            data.push(obj)
        }
        return data;
    },
    inputMen(e){
        this.setData({
            men:e.detail.value
        })
    },
    bindStart:function (e) {
        let count = e.currentTarget.dataset.count;
        let project = this.data.project;
        let obj = project[count]?project[count]:{};
        obj.start = e.detail.value;
        project[count] = obj
        this.setData({
            project:project
        })
        console.log(this.data.project)
    },
    bindEnd:function (e) {
        let count = e.currentTarget.dataset.count;
        let project = this.data.project;
        let obj = project[count]?project[count]:{};
        obj.end = e.detail.value;
        project[count] = obj
        this.setData({
            project:project
        })
        console.log(this.data.project)
    },
    inputCity(e){
        let count = e.currentTarget.dataset.count;
        let project = this.data.project;
        let obj = project[count]?project[count]:{};
        obj.city = e.detail.value;
        project[count] = obj
        this.setData({
            project:project
        })
        console.log(this.data.project)
    },
    selectAvatar(){
        let url = app.globalData.upload;
        wx.chooseImage({
            success:res=>{
                let tempFilePaths = res.tempFilePaths
                wx.uploadFile({
                    url: url, //仅为示例，非真实的接口地址
                    filePath: tempFilePaths[0],
                    name: 'source',
                    success: res=>{
                        let response = JSON.parse(res.data);
                        console.log(response);
                        if (response.code === 200){
                            this.setData({
                                avatar : response.data.file
                            })
                        }
                    }
                })
            }
        })
    },
    inputProjectName(e){
        let count = e.currentTarget.dataset.count;
        let project = this.data.project;
        let obj = project[count]?project[count]:{};
        obj.name = e.detail.value;
        project[count] = obj
        this.setData({
            project:project
        })
        console.log(this.data.project)
    },
    submit(){
        let {region,
            holp,
            count,
            name,
            type,
            men,
            types,
            project,
            phone,
            avatar} = this.data
        let data = {
            region:region,
            holp:holp,
            count:count,
            name:name,
            type:type,
            men:men,
            types:types,
            phone:phone,
            project:project,
            avatar:avatar
        };
        if (!this.cityCheck(region)){
            this.showError('请填写完整籍贯')
            return
        }
        if (!this.cityCheck(holp)){
            this.showError('请填写完整期望工作城市')
            return
        }
        if (types && region && holp && count && name && type && men && project && avatar && phone){
            wx.getLocation({
                type: 'wgs84',
                success: (res)=> {
                    let latitude = res.latitude
                    let longitude = res.longitude
                    this.setData({
                        lat:latitude,
                        lng:longitude
                    })
                }
            })
            let {lat,lng} = this.data
            if (lat && lng){
                data.lat = lat
                data.lng = lng
                app.axios('/craftsman','POST',response=>{
                    if (response){
                        wx.redirectTo({
                            url: '/pages/regSuccess/index'
                        })
                    }
                },data)
            } else {
                wx.showModal({
                    title: '错误',
                    content: '请允许获取位置',
                    showCancel: false
                })
            }
        }
        else {
            if (!avatar){
                this.showError('请上传头像')
                return
            }
            if (!name){
                this.showError('请填写姓名')
                return
            }
            if (!phone){
                this.showError('请填写电话号码')
                return
            }
            if (!types){
                this.showError('请选择大工种')
                return
            }

            if (!type){
                this.showError('请选择工种')
                return
            }
            if (!men){
                this.showError('请填写班组人数')
                return
            }
            if (!project){
                this.showError('请填写至少一项过往工程')
                return
            }


        }

    },
    showError(msg){
        wx.showModal({
            title: '错误',
            content: msg,
            showCancel: false
        })
    },
    cityCheck(region){
        let res = true
        region.forEach(item=>{
            if (item.code === undefined || item.code==0){
                res = false
            }
        });
        return res
    }
});
